package com.hieu.android.designpattern.observer;

import com.hieu.android.designpattern.R;
import com.hieu.android.designpattern.observer.MyTopic;
import com.hieu.android.designpattern.observer.Observer;
import com.hieu.android.designpattern.observer.Subject;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.SystemClock;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;


public class ObserverMainActivity extends Activity {

	Button btnSubject;
	Button btnObserver1;
	Button btnObserver2;
	Button btnObserver3;
	TextView tvSubject;
	TextView tvObserver1;
	TextView tvObserver2;
	TextView tvObserver3;
	
	private long startTime = 0L;
	private Handler myHandler = new Handler();
	private long timeInMillies = 0L;
	private long timeSwap = 0L;
	private long finalTime = 0L;
	
	SubjecState mstateS;
	MyTopic mtopic;
	Observer mobj1;
	Observer mobj2;
	Observer mobj3;
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_observer_main);
		
		btnSubject = (Button) findViewById(R.id.button1);
		btnObserver1 = (Button) findViewById(R.id.button2);
		btnObserver2 = (Button) findViewById(R.id.button3);
		btnObserver3 = (Button) findViewById(R.id.button4);
		tvSubject = (TextView) findViewById(R.id.textView1);
		tvObserver1 = (TextView) findViewById(R.id.textView2);
		tvObserver2 = (TextView) findViewById(R.id.textView3);
		tvObserver3 = (TextView) findViewById(R.id.textView4);

		mstateS = SubjecState.start;
		ImplementObserver();

		btnSubject.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				onClickOnSubject();
			}
		});
	}

	
	public void onClickOnSubject() {
	    switch (mstateS) {
		case start:
			Log.i("HIEU-LOG", "setOnClickListener --- startButton");
			startTime = SystemClock.uptimeMillis();
			myHandler.postDelayed(updateTimerMethod, 0);
			btnSubject.setText(R.string.Subject_Pause);
			mstateS = SubjecState.pause;
			mtopic.postMessage("Start");
			break;

		case pause:
			Log.i("HIEU-LOG", "setOnClickListener --- pauseButton");
			timeSwap += timeInMillies;
			myHandler.removeCallbacks(updateTimerMethod);
			btnSubject.setText(R.string.Subject_Stop);
			mstateS = SubjecState.stop;
			mtopic.postMessage("Pause");
			break;

		case stop:
			Log.i("HIEU-LOG", "setOnClickListener --- stopButton");
			btnSubject.setText(R.string.Subject_Start);
			mstateS = SubjecState.start;
			mtopic.postMessage("Stop");
			tvSubject.setText("");
			break;

		default:
			break;
		}
	}
	
	
	public void ImplementObserver() {
		//create observers
		mtopic = new MyTopic(R.id.button1, String.valueOf(R.string.topic_name));
		mobj1 = new MyTopicSubscriber("Obj1", R.id.textView2);
		mobj2 = new MyTopicSubscriber("Obj2", R.id.textView3);
		mobj3 = new MyTopicSubscriber("Obj3", R.id.textView4);
		
		//register observers to the subject
		mtopic.register(mobj1);
		mtopic.register(mobj2);
		mtopic.register(mobj3);
		
		//attach observer to subject
		mobj1.setSubject(mtopic);
		mobj2.setSubject(mtopic);
		mobj3.setSubject(mtopic);
	}

	
	private Runnable updateTimerMethod = new Runnable() {
		public void run() {
			int seconds = (int) (finalTime / 1000);
			int minutes = seconds / 60;
			int milliseconds = (int) (finalTime % 1000);
			
			seconds = seconds % 60;
			timeInMillies = SystemClock.uptimeMillis() - startTime;
			finalTime = timeSwap + timeInMillies;
			tvSubject.setText("" + String.format("%02d", minutes) + ":" + String.format("%02d", seconds) + ":" + String.format("%03d", milliseconds));
			myHandler.postDelayed(this, 0);
		}
	};

	
	enum SubjecState {
		start,
		pause,
		stop,
	}

	
	public class MyTopicSubscriber implements Observer {
		private String name;
		private int idUI;
		private Subject topic;
		
		public MyTopicSubscriber(String nm, int idUI) {
			this.name=nm;
			this.idUI = idUI;
		}

		public String getSubcriberName() {
			return name;
		}
		
		@Override
		public void update() {
			String msg = (String) topic.getUpdate(this);
			((TextView)findViewById(this.idUI)).setText(msg);
		}

		@Override
		public void setSubject(Subject sub) {
			this.topic=sub;
		}
	}


	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		return super.onCreateOptionsMenu(menu);
	}


	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		return super.onOptionsItemSelected(item);
	}
	
	
}
